import { Component, OnInit } from '@angular/core';
import {ServiceLogicService} from '../../service/service-logic.service';
import {OutRecord} from '../../domain/admin';
@Component({
  selector: 'app-approval-outing',
  templateUrl: './approval-outing.component.html',
  styleUrls: ['./approval-outing.component.css']
})
export class ApprovalOutingComponent implements OnInit {
  cols: any[];
  pendings: OutRecord[];     // 待审批外出记录
  processed: OutRecord[];   // 已处理的外出记录
  outRecord: OutRecord = {};
  displayDialog: boolean;
  selectedOutRecord: OutRecord;
  constructor(private logicService: ServiceLogicService) { }

  onRowSelect(event) {
    this.outRecord = this.cloneLeaveRecord(event.data);
    this.displayDialog = true;
  }
  save() {
    const outRecords = [...this.pendings];

    outRecords[this.pendings.indexOf(this.selectedOutRecord)] = this.outRecord;
    this.outRecord.state = parseInt(this.outRecord.state, 0);
    switch (this.outRecord.state) {
      case 0:
        this.outRecord.des = '未通过';
        break;
      case 2:
        this.outRecord.des = '通过';
        break;
      case 1:
        this.outRecord.des = '审核中';
        break;
      default:
        break;
    }
    console.log(this.outRecord);

    if (this.outRecord.state === 2 || this.outRecord.state === 0) {
      const index = this.pendings.indexOf(this.selectedOutRecord);
      console.log(index);
      console.log(this.pendings);
      this.pendings = this.pendings.filter((nmnm, i) => i !== index);
      // this.pendings = leaveRecords;
      const processedRecords = [...this.processed];
      processedRecords.push(this.outRecord);
      this.processed = processedRecords;
    }
    this.logicService.approvalOutDaysLessThanThree(this.outRecord).then(res => {this.outRecord = null;
      this.displayDialog = false; });
  }
  cloneLeaveRecord(c: OutRecord): OutRecord {
    const outRecord = {};
    for (const prop in c) {
      if (c.hasOwnProperty(prop)) {
        outRecord[prop] = c[prop];
      }
    }
    return outRecord;
  }

  ngOnInit() {
    this.logicService.getOutDayLessThanThree(1).then(res => this.pendings = res);
    this.logicService.getOutDayLessThanThree(5).then(res => this.processed = res);
    this.cols = [
      {field: 'admin_id', header: 'admin_id'},
      {field: 'satrt_time', header: 'satrt_time'},
      {field: 'end_time', header: 'end_time'},
      {field: 'days', header: 'days'},
      {field: 'reason', header: 'reason'},
      {field: 'des', header: 'des'},
    ];
  }

}
